<#assign tit><#if _oper='add'>新增<#else>修改</#if>商品退货数量</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<div style="width:480px; float:left;">

  <form id="fm_input" method="post" action="<#if _oper='add'>_add<#else>_edit</#if>" onsubmit="return post(this);">
    <div class="kbox">
      <table class="input">
      	<#if _oper='edit'>
        <tr>
          <th width="25%">退货单号</th>
          <td width="75%">${pssPurcReturn.returnID!}</td>
    		<input type="hidden" name="shopCode" value="${pssPurcReturn.shopCode!}">
    		<input type="hidden" name="returnID" value="${pssPurcReturn.returnID!}"
        </tr>
        </#if>
        <tr>
          <th width="25%">退货日期</th>
          <td width="75%"><input type="text" class="date" onclick="cal(this);" name="returnDate" value="${pssPurcReturn.returnDate!}">${pssPurcReturn.returnDate!}</td>
        </tr>
      </table>
    <input type="hidden" id="listIndex" value="0">
    <#if _oper='add'><div class="button"><input type="button" class="opwin" value="+添加商品" onclick="selectProduct();"/></div></#if>
    <div class="lsinfo">退货商品明细</div>
  	<div id="div_list">
		<table class="list" id = "testId">
		  <tr>
		    <th width="10%">商品编码</th>
		    <th width="10%">商品名称</th>
		    <th width="10%">单位</th>
		    <th width="10%">数量</th>
    		<#if _oper='add'><th width="10%">操作</th></#if>
		  </tr>
		  <#if list??><#list list as c>
		  <tr<#if c_index%2==1> class="line2"<#else> class="line1"</#if>>
		    <td>${c.productCode!}</td>
		    <td><@value type="ProductcodeList" key="${c.productCode!}"/></td>
		    <td><@value type="productUnit" key="${c.unit!}"/></td>
		    <td><input style="text" id="lstProduct[${c_index!}].qty" name="lstProduct[${c_index!}].qty" value="${c.quantity!}" vtp="number" size="14" maxlength="16" onblur="changeQty(${c_index!})"></td>
		    <#if _oper='add'><td class="oper"><a class="del" href="javascript:cdel(${c_index!})">删除</a></td></#if>
			<input type="hidden" name="lstProduct[${c_index!}].productCode" value="${c.productCode!}">
			<input type="hidden" name="lstProduct[${c_index!}].id" value="${c.autoID!}">
		  </tr>
		  </#list></#if>
		</table>
  	</div>
    </div>
    <div class="button"><input type="submit" value="提 交"></div>
  </form>
</div>
<script type="text/javascript">
function selectProduct(c) {
  openWin("getProduct", 700, 400);
}

function addProduct(productInfo) {
    var tb = getEl("testId");
    if(!tb) return;
    var i, tr, td;
    i=tb.rows.length;

    for(k=1; k<tb.rows.length; k++) {
		// 重复校验
    	if(tb.rows[k].cells[0].innerHTML.replace(/<.+?>/gim,'')==productInfo.cells[0].innerHTML.replace(/<.+?>/gim,'')) {
        	window.alert("请不要重复选择该商品！"); 
        	return null;
    	}
    }

    var j;
    j = Number(getEl("listIndex").value);
    j = j + 1;
    getEl("listIndex").value = j;
    tr = tb.insertRow(-1);
    tr.className = "line"+((i%2));
    tr.id=i;
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[0].innerHTML.replace(/<.+?>/gim,'') + '<input type="hidden" name="lstProduct[' + j + '].productCode" value='+productInfo.cells[0].innerHTML.replace(/<.+?>/gim,'')+'>';
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[1].innerHTML.replace(/<.+?>/gim,'');
    td = tr.insertCell(-1);
    td.innerHTML = productInfo.cells[2].innerHTML.replace(/<.+?>/gim,'') + '<input type="hidden" name="lstProduct[' + j + '].price" value='+productInfo.cells[3].innerHTML.replace(/<.+?>/gim,'')+'>';
    td = tr.insertCell(-1);
    td.innerHTML = '<input style="text" id="lstProduct[' + j + '].qty" name="lstProduct[' + j + '].qty" vtp="number" value="1" size="14" maxlength="16" onblur="changeQty(' + j + ')">';
    td = tr.insertCell(-1);
    td.className = "oper"
    td.innerHTML = '<a class="del" href="javascript:void(0)" onclick="cdel(this)">删除</a>';
}

// 数量输入后check
function changeQty(j) {

    var qty = getEl("lstProduct[" + j + "].qty");
	// 数字校验
    if(!chkValueType(qty.value, 'number')) {
        try {qty.focus();} catch(e) {}
        return null;
    }
}

function cdel(src) {
    var tb = getEl("testId");
    if(!tb) return;
	var tr=document.getElementById(src.parentElement.parentElement.id);
	tr.parentNode.removeChild(tr); 
}

function save(fm) {
    var tb = getEl("testId");
    if(!tb) return;
    var i;
    i=tb.rows.length;
    if(i==1) {
      window.alert("没有选择退货的商品"); 
      return false;
    }
        
    postForm("_add", fm, saveRet);
    return false;
}

function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) {
    	getEl("fm_input").reset(); //window.location = "print?id="+data.data;
    	var tb = getEl("testId");
    	if(!tb) return;
    	var rowNum = tb.rows.length;
    	for (i=1;i<rowNum;i++){
         	tb.deleteRow(i);
         	rowNum=rowNum-1;
         	i=i-1;
     	}
    }
}
</script>
</@page.html>